﻿/*
-----------------------------------------------------------------------------
This source file is part of Cell Cloud.

Copyright (c) 2009-2012 Cell Cloud Team - cellcloudproject@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/

#include "CellStableHeaders.h"
#include "CellDialectEnumerator.h"
#include "CellDialect.h"
#include "CellException.h"
#include "CellLoggerManager.h"

namespace cell
{

//-----------------------------------------------------------------------
template<> DialectEnumerator* Singleton<DialectEnumerator>::ms_Singleton = NULL;

//-----------------------------------------------------------------------
DialectEnumerator* DialectEnumerator::getSingletonPtr(void)
{
	return ms_Singleton;
}
DialectEnumerator& DialectEnumerator::getSingleton(void)
{  
	assert(ms_Singleton);
	return (*ms_Singleton);
}

//---------------------------------------------------------------------------
DialectEnumerator::DialectEnumerator(void)
{
	addFactory(&mActionFactory);
}
//---------------------------------------------------------------------------
DialectEnumerator::~DialectEnumerator(void)
{
	removeFactory(&mActionFactory);

	for (Instances::iterator i = mInstances.begin(); i != mInstances.end(); ++i)
	{
		// 销毁实例
		for (Factories::iterator f = mFactories.begin(); f != mFactories.end(); ++f)
		{
			if ((*f)->getMetaData().name == i->first)
			{
				for (Dialects::iterator d = i->second->begin(); d != i->second->end(); ++d)
				{
					(*f)->destroyInstance((*d));
				}

				break;
			}
		}

		Dialects* list = i->second;
		CELL_DELETE_T(list, Dialects, MEMCATEGORY_GENERAL);
	}
}
//---------------------------------------------------------------------------
void DialectEnumerator::addFactory(DialectFactory* fact)
{
	mFactories.push_back(fact);
	mMetaDataList.push_back(&fact->getMetaData());

	// 日志
	LoggerManager::getSingleton().log("DialectFactory for type '" +
		fact->getMetaData().name + "' registered.");
}
//---------------------------------------------------------------------------
void DialectEnumerator::removeFactory(DialectFactory* fact)
{
	// destroy all instances for this factory
	Instances::iterator i = mInstances.find(fact->getMetaData().name);
	if (i != mInstances.end())
	{
		Dialects* list = i->second;
		for (Dialects::iterator d = list->begin(); d != list->end(); ++d)
		{
			fact->destroyInstance((*d));
		}

		CELL_DELETE_T(list, Dialects, MEMCATEGORY_GENERAL);
		mInstances.erase(i);
	}

	// remove from metadata
	for (MetaDataList::iterator m = mMetaDataList.begin(); m != mMetaDataList.end(); ++m)
	{
		if (*m == &(fact->getMetaData()))
		{
			mMetaDataList.erase(m);
			break;
		}
	}

	// remove from factories
	mFactories.remove(fact);
}
//---------------------------------------------------------------------------
const DialectMetaData* DialectEnumerator::getMetaData(const String& name) const
{
	for (MetaDataList::const_iterator i = mMetaDataList.begin(); 
		i != mMetaDataList.end(); ++i)
	{
		if (name == (*i)->name)
		{
			return *i;
		}
	}

	CELL_EXCEPT(Exception::ERR_ITEM_NOT_FOUND,
		"No metadata found for dialect of type '" + name + "'",
		"DialectEnumerator::getMetaData");
}
//---------------------------------------------------------------------------
Dialect* DialectEnumerator::createDialect(const String& name, const String& tracker)
{
	Dialect* inst = NULL;

	for (Factories::iterator i = mFactories.begin(); i != mFactories.end(); ++i)
	{
		if ((*i)->getMetaData().name == name)
		{
			inst = (*i)->createInstance(tracker);
			break;
		}
	}

	if (!inst)
	{
		// Error!
		CELL_EXCEPT(Exception::ERR_ITEM_NOT_FOUND, 
			"No factory found for dialect of type '" + name + "'",
			"DialectEnumerator::createDialect");
	}

	Instances::iterator iter = mInstances.find(name);
	if (iter != mInstances.end())
	{
		(iter->second)->push_back(inst);
	}
	else
	{
		Dialects* list = CELL_NEW_T(Dialects, MEMCATEGORY_GENERAL);
		list->push_back(inst);
		mInstances[name] = list;
	}

	return inst;
}
//---------------------------------------------------------------------------
void DialectEnumerator::destroyDialect(Dialect* dialect)
{
	// Erase instance from map
	Instances::iterator iter = mInstances.find(dialect->getName());
	if (iter != mInstances.end())
	{
		Dialects* list = iter->second;
		list->remove(dialect);

		if (list->empty())
		{
			CELL_DELETE_T(list, Dialects, MEMCATEGORY_GENERAL);
			mInstances.erase(iter);
		}
	}

	// Find factory to destroy
	for (Factories::iterator i = mFactories.begin(); i != mFactories.end(); ++i)
	{
		if ((*i)->getMetaData().name == dialect->getName())
		{
			(*i)->destroyInstance(dialect);
			break;
		}
	}
}

} // end namespace cell
